// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "Input/Q3D_CPPC_InputState.h"
#include "GameFramework/InputSettings.h"
#include "Hit/Q3D_CPPU_Hit.h"
#include "Input/Q3D_CPPI_InputState.h"
#include "Input/Q3D_CPPS_Input_ActionMapping.h"
#include "Input/Q3D_CPPS_Input_AxisMapping.h"
#include "Logger/Q3D_CPPU_Logger.h"

// Sets default values for this component's properties
UQ3D_CPPC_InputState::UQ3D_CPPC_InputState()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}

// Called when the game starts
void UQ3D_CPPC_InputState::BeginPlay()
{
	Super::BeginPlay();

	// ...

	this->UQ3D_CPPC_InputState::Q3D_F_Model_Init();
}

// Called every frame
void UQ3D_CPPC_InputState::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UQ3D_CPPC_InputState::Q3D_F_Model_Init()
{
	AActor *ptr_a_owner = Cast<AActor>(this->GetOwner());
	IQ3D_CPPI_InputState *ptr_i_input_state_owner = Cast<IQ3D_CPPI_InputState>(ptr_a_owner);
	if (!ptr_i_input_state_owner)
	{
		Q3D_LOG(Warning, "Cast_To_Q3D_CPPI_InputState_Failed");
		return;
	}
	// 从 Owner 读 ActionMapping
	UDataTable *ptr_dt_input_action_mapping_tmp;
	ptr_i_input_state_owner->Execute_Q3D_IF_InputState_InitDT_GetActionMapping(ptr_a_owner, ptr_dt_input_action_mapping_tmp);
	// 往 InputState 中写入初始数据
	this->Q3D_F_Model_SetActionMapping(ptr_dt_input_action_mapping_tmp);
	// 从 Owner 读 AxisMapping
	UDataTable *ptr_dt_input_axis_mapping_tmp;
	ptr_i_input_state_owner->Execute_Q3D_IF_InputState_InitDT_GetAxisMapping(ptr_a_owner, ptr_dt_input_axis_mapping_tmp);
	// 往 InputState 中写入初始数据
	this->Q3D_F_Model_SetAxisMapping(ptr_dt_input_axis_mapping_tmp);
}

void UQ3D_CPPC_InputState::Q3D_F_Model_SetActionMapping(UDataTable *ptr_dt_input_action_mapping)
{
	if (!ptr_dt_input_action_mapping)
	{
		Q3D_LOG(Log, "Invalid_Ptr_DT_Input_ActionMapping");
		return;
	}

	// 准备好 UInputSettings
	UInputSettings *ptr_u_input_settings = const_cast<UInputSettings *>(GetDefault<UInputSettings>());

	// DataTable 的一种读法：
	// 取 RowName
	TArray<FName> array_name_key = ptr_dt_input_action_mapping->GetRowNames();
	// 循环 RowName
	for (auto &one : array_name_key)
	{
		// 取 Row
		FString string_context;
		FQ3D_CPPS_Input_ActionMapping *ptr_s_input_action_mapping = ptr_dt_input_action_mapping->FindRow<FQ3D_CPPS_Input_ActionMapping>(one, string_context);
		if (ptr_s_input_action_mapping)
		{
			// Q3D_LOG( Verbose, "Load_" \
			// +	StaticEnum<EQ3D_CPPE_PropertyName>()->GetNameStringByValue( (uint8)(ptr_s_input_action_mapping->Enum_PropertyName) ) \
			// +	"(" + FString::FromInt( ptr_s_input_action_mapping->Int_Min ) \
			// +	"," + FString::FromInt( ptr_s_input_action_mapping->Int_Max ) \
			// +	"," + ( ptr_s_input_action_mapping->Bool_Active ? "true" : "false" ) \
			// +	"," + StaticEnum<EQ3D_CPPE_ActionMapping>()->GetNameStringByValue( (uint8)(ptr_s_input_action_mapping->Enum_ActionMapping) ) \
			// +	"," + FString::FromInt( ptr_s_input_action_mapping->Int_Value ) \
			// +	")" \
			// );
			ptr_u_input_settings->AddActionMapping(
				FInputActionKeyMapping(
					ptr_s_input_action_mapping->KeyMapping_ActionName,
					ptr_s_input_action_mapping->KeyMapping_Key,
					ptr_s_input_action_mapping->KeyMapping_Shift,
					ptr_s_input_action_mapping->KeyMapping_Ctrl,
					ptr_s_input_action_mapping->KeyMapping_Alt,
					ptr_s_input_action_mapping->KeyMapping_Cmd));
		}
	}
	ptr_u_input_settings->SaveKeyMappings();
	Q3D_LOG(Log, "InputState_Inited_ActionMapping");
}

void UQ3D_CPPC_InputState::Q3D_F_Model_SetAxisMapping(UDataTable *ptr_dt_input_axis_mapping)
{
	if (!ptr_dt_input_axis_mapping)
	{
		Q3D_LOG(Log, "Invalid_Ptr_DT_Input_AxisMapping");
		return;
	}

	// 准备好 UInputSettings
	UInputSettings *ptr_u_input_settings = const_cast<UInputSettings *>(GetDefault<UInputSettings>());

	// 循环 RowMap
	for (auto one : ptr_dt_input_axis_mapping->GetRowMap())
	{
		// 取 RowName
		FString name_key = one.Key.ToString();
		// 取 Row
		FQ3D_CPPS_Input_AxisMapping *ptr_s_input_axis_mapping = (FQ3D_CPPS_Input_AxisMapping *)(one.Value);
		// Q3D_LOG( Verbose, "Load_" \
		// +	StaticEnum<EQ3D_CPPE_PropertyName>()->GetNameStringByValue( (uint8)(ptr_s_input_axis_mapping->Enum_PropertyName) ) \
		// +	"(" + FString::FromInt( ptr_s_input_axis_mapping->Int_Min ) \
		// +	"," + FString::FromInt( ptr_s_input_axis_mapping->Int_Max ) \
		// +	"," + ( ptr_s_input_axis_mapping->Bool_Active ? "true" : "false" ) \
		// +	"," + StaticEnum<EQ3D_CPPE_AxisMapping>()->GetNameStringByValue( (uint8)(ptr_s_input_axis_mapping->Enum_AxisMapping) ) \
		// +	"," + FString::FromInt( ptr_s_input_axis_mapping->Int_Value ) \
		// +	")" \
		// );
		ptr_u_input_settings->AddAxisMapping(
			FInputAxisKeyMapping(
				ptr_s_input_axis_mapping->KeyMapping_AxisName,
				ptr_s_input_axis_mapping->KeyMapping_Key,
				ptr_s_input_axis_mapping->KeyMapping_Scale));
	}
	ptr_u_input_settings->SaveKeyMappings();
	Q3D_LOG(Log, "InputState_Inited_AxisMapping");
}
